/* eslint-disable camelcase */
import React, {
  useMemo, useEffect, createContext, useContext, useState,
} from 'react';
import { inject, observer } from 'mobx-react';
import { injectIntl } from 'react-intl';
import { DataSet } from 'choerodon-ui/pro';

import { axios } from '@choerodon/master';
import {
  useParams, useHistory, useLocation,
} from 'react-router';
// @ts-ignore
import queryString from 'query-string';
import useStore from './useStore';
import SiderDs from './siderDataSet';
import TreeDs from './treeDataSet';
import PipelineDetailDs from './pipelineDetailDataset';
import RecordDetaiDs from './recordDetailDataSet';
import { transformBasicConfDataToUse } from '../../deployment-operation/utils/index';

interface ContextProps{
    prefixCls:string,
    intlPrefix:string,
    dyDetailStore:any,
    siderDs:DataSet,
    treeDs:DataSet,
    projectId:string,
    realName:string,
    pipDetailDs:DataSet,
    recordDetailDs:DataSet,
    intl:object,
    basicConfigurationData:any,
    formatMessage:Function,
    handleQuery:(id:string)=>void,
}
const Store = createContext({} as ContextProps);
export function useDetailStore() {
  return useContext(Store);
}
export const StoreProvider = injectIntl(inject('AppState')(observer((props:any) => {
  const {
    AppState: {
      currentMenuType: { projectId },
      userInfo: { realName },
    },
    intl: { formatMessage },
    children,
  } = props;
  const { search } = useLocation();
  const { recordId, audit_flag } = queryString.parse(search);
  const [stageData, setStageData] = useState<any>([]);
  const [basicConfigurationData, setBasicConfigurationData] = useState<any>({});
  // 使用hooks对params参数进行接收
  const { id } = useParams<{ id: string }>();
  const dyDetailStore = useStore();
  // dataSet尽量只在初始化的时候传递参数
  const siderDs = useMemo(() => new DataSet(SiderDs({
    projectId,
    id,
    dyDetailStore,
  })), [projectId, id]);
  const treeDs = useMemo(() => new DataSet(TreeDs({ projectId, id })), [projectId]);
  const pipDetailDs = useMemo(() => new DataSet(PipelineDetailDs({
    projectId,
    id,
  })), [projectId, id]);
  const recordDetailDs = useMemo(() => new DataSet(RecordDetaiDs(projectId)), [projectId]);
  // 根据是否存在查询recordId重新查询待审核的记录
  useEffect(() => {
    if (audit_flag) {
      dyDetailStore.setAuditFlag(true);
      siderDs.setQueryParameter('audit_flag', true);
      siderDs.query();
    } else {
      siderDs.query();
    }
  }, [id]);
  useEffect(() => {
    async function asyncFunc() {
      const res = id && await axios.get(`/devops/v1/projects/${projectId}/pipelines/${id}`);
      setBasicConfigurationData(transformBasicConfDataToUse(res));
      setStageData(res.stageList);
    }
    asyncFunc();
  }, []);
  const value = {
    ...props,
    intlPrefix: 'c7ncd.continuous.deployment.detail',
    prefixCls: 'c7ncd-continuous-deployment-detail',
    formatMessage,
    dyDetailStore,
    siderDs,
    projectId,
    realName,
    pipDetailDs,
    recordDetailDs,
    treeDs,
    basicConfigurationData,
  };
  return (
    <Store.Provider value={value}>
      {children}
    </Store.Provider>
  );
})));
